import * as index from '@/api/index.js'
export default {
	data() {
		return {
			menu: uni.getMenuButtonBoundingClientRect(),
			token:uni.getStorageSync('token'),
			tabsList: [{
					title: '全部订单',
					status: ''
				},
				{
					title: '进行中订单',
					status: 1
				},

				{
					title: '已完成订单',
					status: 2
				}
			],
			tabsIndex: 0,
			list: [],
			form: {
				status: '',
				page: 1
			},
			loading:false,
			loginInfo: {
				mobilecode: '',
				code: ''
			}, //登陆参数
			userInfo: {
				mobile: ''
			},
		}
	},

	onShow() {
		this.token = uni.getStorageSync('token')
		
	},
	onLoad(option) {
		if(uni.getStorageSync('token')){
			this.form.page = 1
			this.list = []
			this.getMyOrderList()
		}
	},
	onPullDownRefresh(){
		this.form.page=1
		this.list = []
		this.getMyOrderList()
	},
	onReachBottom(){
		this.form.page+=1
		this.getMyOrderList()
	},
	methods: {
		getUserInfo() {
			if (uni.getStorageSync('token')) {
				index.getUserInfo().then(res => {
					this.userInfo = res.data
				})
			}
		},
		tabsChange(index) {
			if(this.token){
				if (this.tabsIndex != index) {
					this.tabsIndex = index
					this.form.status = this.tabsList[this.tabsIndex].status
					this.form.page = 1
					this.list = []
					this.getMyOrderList()
				}
			}
		},
		getMyOrderList() {
			this.loading = false
			index.getMyOrderList(this.form).then(res => {
				res.data.data.forEach(item=>{
					console.log(typeof item.coupon_price);
				})
				this.loading = true
				this.list.push(...res.data.data)
			})
		},
		jump(item){
			this.$tools.navigate('/pages/user/order/orderDetail/index?id='+item.id)
		},
		goPay(item){
			let that = this
			index.goPay({order_id:item.id}).then(res=>{
				// this.$tools.toast('支付成功')
				uni.requestPayment({
					provider: res.data.provider,
					timeStamp: res.data.timeStamp,
					nonceStr: res.data.nonceStr,
					package: res.data.package,
					signType: res.data.signType,
					paySign: res.data.paySign,
					success: function(res) {
						that.$tools.toast('支付成功')
						that.form.page = 1
						that.list = []
						that.getMyOrderList()
					},
					fail: function(err) {
						that.$tools.toast('支付失败')
					}
				});
				
			})
		},
		workTime(time) {
			let date = new Date(time * 1000);
			let year = date.getFullYear();
			// 在日期格式中，月份是从0开始的，因此要加0，使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
			let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
			let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
			let hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
			let minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
			// let seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
			// 拼接
			// return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
			return year + "-" + month + "-" + day + " " + hours + ":" + minutes;
		},
		getPhoneNumber(e) {
			let that = this
			// this.getwxCode()
			that.loginInfo.mobilecode = e.detail.code
			uni.login({
				provider: 'weixin',
				onlyAuthorize: true,
				success: function(loginRes) {
					that.loginInfo.code = loginRes.code
					if (that.loginInfo.mobilecode) {
						that.wxLogin()
					}
				}
			});
		},
		wxLogin() {
			let that = this
			index.wxLogin(this.loginInfo).then(res => {
				uni.setStorage({
					key: 'userInfo',
					data: res.data.userinfo,
					success: function() {
						that.userInfo = res.data.userinfo
						// uni.$emit('company', {
						// 	msg: '页面更新'
						// })
					}
				})
				uni.setStorage({
					key: 'token',
					data: res.data.userinfo.token,
					success: function() {
						that.token = res.data.userinfo.token
						that.getUserInfo()
						uni.showToast({
							icon: 'none',
							title: '登录成功',
							duration: 2000
						});
						that.form.page = 1
						that.list = []
						that.getMyOrderList()
					}
				});
			})
		},
	}
}